package com.qyhl.webtv.module_user.login.wx;

import android.app.Activity;

import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

public class WXLoginPresenter implements WXLoginContract.WXLoginPresenter {
    private WXLoginContract.WXLoginView view;
    private WXLoginContract.WXLoginModel model;

    private Map<String, String> map;

    public WXLoginPresenter(WXLoginContract.WXLoginView view) {
        this.view = view;
        model = new WXLoginModel(this);
    }

    @Override
    public void wxLogin(Activity activity) {
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                WXLoginPresenter.this.map = map;
                model.wxLogin(map.get("uid"));
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                view.wxLoginError("微信授权失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
            }
        });
    }

    @Override
    public void wxLoginError(String msg) {
        view.wxLoginError(msg);
    }


    @Override
    public void wxRegister(String openid) {
        view.wxRegister(map, openid);
    }

    @Override
    public void loginSuccess(UserInfoBean data) {
        view.loginSuccess(data);
    }
}
